
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[EBAS_F_SIKKERHED_HENT_ADMIN_RETTIGHED]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[EBAS_F_SIKKERHED_HENT_ADMIN_RETTIGHED]
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE FUNCTION [DBO].[EBAS_F_SIKKERHED_HENT_ADMIN_RETTIGHED] (@session uniqueidentifier, @currentTime datetime)
RETURNS int
WITH ENCRYPTION	
AS
BEGIN
	DECLARE 
		@result int,
		@adminRet int,
		@type uniqueidentifier
		
	SET @type = ( 	SELECT SIKKERHED_OBJEKT_TYPE_GUID 
			FROM EBAS_SIKKERHED_SESSION 
			WHERE GUID = @session )
					
	SET @adminRet = (
				SELECT IsNull(MAX(RE.RETTIGHEDER), 0)
				FROM EBAS_SIKKERHED_BRUGER B, EBAS_SIKKERHED_RELATION R,
				EBAS_SIKKERHED_GRUPPE G, EBAS_SIKKERHED_OBJEKT_TYPE T,
				EBAS_SIKKERHED_RETTIGHEDER RE
				WHERE B.GUID = R.SIKKERHED_BRUGER_GUID
				AND R.SIKKERHED_GRUPPE_GUID = G.GUID
				AND G.GUID = RE.SIKKERHED_GRUPPE_GUID
				AND RE.SIKKERHED_OBJEKT_TYPE_GUID = T.GUID
				AND T.NAVN = 'EBASAdmin'
				AND B.GUID = ( 	SELECT TOP 1 SIKKERHED_BRUGER_GUID 
						FROM EBAS_SIKKERHED_SESSION
						WHERE GUID = @session
						AND DEAKTIVERET = 0
						AND SLUT_DATO > @currentTime) 				
			)			
	
	RETURN (@adminRet)
END
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO
